package com.evil.common.file.enums;

import com.evil.common.core.enums.BaseEnum;
import com.evil.common.core.exception.BusinessException;
import lombok.AllArgsConstructor;
import lombok.Getter;

import java.util.Arrays;
import java.util.Optional;

/**
 * 文件存储服务商
 *
 * @author liyang
 */
@Getter
@AllArgsConstructor
public enum FileServiceProviderEnum {
    /**
     * 文件存储服务商
     */
    LOCAL(1, "本地"),
    ALIYUN(2, "阿里云"),
    ;

    private final int id;
    private final String name;

    public static Optional<FileServiceProviderEnum> isExistById(Integer id) {
        if (null == id) {
            return Optional.empty();
        }
        return Arrays.stream(values()).filter(e -> e.id == id).findFirst();
    }

    public static FileServiceProviderEnum findById(Integer id) {
        return FileServiceProviderEnum.isExistById(id).orElseThrow(FileServiceProviderEnum::error);
    }

    public static BusinessException error() {
        return new BusinessException(BaseEnum.ERROR_PARAMETER, "文件存储服务商类型错误");
    }
}
